package org.geeks.gsredisson.api;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("cache")
public class CacheApi {

  @GetMapping("-0")
  @Cacheable(value = "cache0", cacheManager = "cacheManager")
  public List<Integer> cache0() {
    log.info("Execute cache0 ~");

    return Lists.newArrayList(1, 3, 5, 7, 9);
  }

  @GetMapping("-1")
  @Cacheable(value = "cache1", cacheManager = "cacheManager")
  public List<Integer> cache1() {
    log.info("Execute cache1 ~");

    return Lists.newArrayList(0, 2, 4, 6, 8);
  }

  @GetMapping("dynamic")
  @Cacheable(
      cacheNames = "dynamic",
      cacheManager = "cacheManager",
      key = "'CacheApi.dynamic.' + #name")
  public Map<String, String> dynamic(String name) {
    log.info("Execute dynamic ~");

    Map<String, String> result = Maps.newHashMap();
    result.put("msg", "Hello, " + name);

    return result;
  }
}
